package com.csea.tools.controller.easypoi;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.csea.tools.tables.*;
import com.csea.tools.util.FileUtilTest;
import com.csea.tools.util.WorkBookDownUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.csea.tools.util.ImageFileStream.getFileStream;

/**
 * easypoi 文档地址：https://opensource.afterturn.cn/doc/easypoi.html#3
 *
 * @author Csea
 * @title
 * @date 2019/12/27 15:39
 */
@RestController
@RequestMapping("/easypoi")
@Api(description = "EasyPoi下载Excel")
@Slf4j
public class EasypoiController {


    /**
     * 导出基本的Excel
     *
     * @param response
     */
    @GetMapping("/export/Excel")
    @ApiOperation(value = "导出基本的Excel")
    public void exportExcel(HttpServletResponse response) {
        List<StudentTable> list = new ArrayList<>();
        list.add(new StudentTable("001", "小王1", 1, new Date(), LocalDateTime.now()));
        list.add(new StudentTable("002", "小王2", 2, new Date(), LocalDateTime.now()));
        list.add(new StudentTable("003", "小王3", 1, new Date(), LocalDateTime.now()));
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("计算机一班学生", "学生"),
                StudentTable.class, list);
        WorkBookDownUtil.workBookDown(workbook, response, "基本表格.xls");
    }

    /**
     * 导出 1:N Excel
     */
    @ApiOperation(value = "导出一对多的数据Excel")
    @GetMapping("/export/oneToMore")
    public void exportOneToMoreExcel(HttpServletResponse response) {
        List<CourseTable> list = new ArrayList<>();
        List<StudentTable> students = new ArrayList<>();
        students.add(new StudentTable("002", "小王2", 1, new Date(), LocalDateTime.now()));
        students.add(new StudentTable("001", "小王1", 1, new Date(), LocalDateTime.now()));
        list.add(new CourseTable("0001", "化学", new TeacherTable("001", "王老师"), students));
        list.add(new CourseTable("0001", "化学", new TeacherTable("001", "王老师"), students));
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("2412312", "测试"),
                CourseTable.class, list);
        WorkBookDownUtil.workBookDown(workbook, response, "1_N表格.xls");
    }

    @ApiOperation(value = "包含图片的Excel导出")
    @GetMapping("/export/imageExcel")
    public void exportImageExcel(HttpServletResponse response) {
        List<ImageTable> list = new ArrayList<>();
        list.add(new ImageTable("0001", "图片1", getFileStream("http://47.106.250.37:6010/Manager/images/logo.jpg")));
        list.add(new ImageTable("0002", "图片2", getFileStream("http://47.106.250.37:6010/Manager/images/logo.jpg")));
        list.add(new ImageTable("0003", "图片3", getFileStream("http://47.106.250.37:6010/Manager/images/logo.jpg")));
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), ImageTable.class, list);
        WorkBookDownUtil.workBookDown(workbook, response, "带有图片的excel导出.xls");
    }

    @ApiOperation(value = "基本的Excel导入")
    @GetMapping("/import/excel")
    public void importExcel() {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        long start = System.currentTimeMillis();
        List<StudentImport> list = ExcelImportUtil.importExcel(
                new File(FileUtilTest.getWebRootPath("import/studentTable.xlsx")), StudentImport.class, params);
        log.info("耗时={}", System.currentTimeMillis() - start);
        log.info("数据长度={}", list.size());
        log.info("第一条数据={}", ReflectionToStringBuilder.toString(list.get(0)));
        log.info("数据列表={}", list.toString());
    }

    @ApiOperation(value = "带图片的Excel导入",notes = "对于这个图片上传他是默认上传到项目磁盘下创建文件存储，如果要上传到阿里云OSS需要另外处理")
    @GetMapping("/import/imageExcel")
    public void importImageExcel() {
        try {
            ImportParams params = new ImportParams();
            List<ImportImage> result = ExcelImportUtil.importExcel(
                    new File(FileUtilTest.getWebRootPath("import/importImage.xls")),
                    ImportImage.class, params);
            for (int i = 0; i < result.size(); i++) {
                log.info("数据是={}", ReflectionToStringBuilder.toString(result.get(i)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
